define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'offline_topics.offline_topics/index',
        add_url: 'offline_topics.offline_topics/add',
        edit_url: 'offline_topics.offline_topics/edit',
        delete_url: 'offline_topics.offline_topics/delete',
        export_url: 'offline_topics.offline_topics/export',
        modify_url: 'offline_topics.offline_topics/modify',
    };

    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: 'id'},
                    {field: 'subjects.title', title: '科目名称'},
                    {field: 'course.title', title: '课程名称'},
                    {field: 'title', title: '题目名称'},
                    {field: 'type', search: 'select', selectList: {"1":"单选题","2":"多选题","3":"判断题"}, title: '题目类型'},
                    {field: 'images', title: '图片地址', templet: ea.table.image},
                    {field: 'create_time', title: '创建时间'},
                    {width: 250, title: '操作', templet: ea.table.tool},
                ]],
            });

            ea.listen();
        },
        add: function () {
            layui.use(['form'], function() {
                var form = layui.form;
                form.on('select(type)', function(data){
                    $('.xxitem').empty()
                    if(data.value == 3){
                        var str = '<label class="layui-form-label">选项</label><div class="layui-input-block" style="margin-top: 10px;"><label class="layui-form-label" style="width: 20px">A、</label><input disabled type="text" name="options-A" class="xxinput"  placeholder="正确" value="正确"></div><div class="layui-input-block" style="margin-top: 10px"><label class="layui-form-label" style="width: 20px">B、</label> <input disabled type="text" name="options-B" class="xxinput"  placeholder="错误" value="错误"></div>'
                        $('.xxitem').append(str);
                    }else {
                        var str = '<label class="layui-form-label">选项</label><div class="layui-input-block xxzj" id="xxzj"><label class="layui-form-label" style="width: 20px">+</label></div><div class="layui-input-block" style="margin-top: 10px"><label class="layui-form-label" style="width: 20px">A、</label><input type="text" name="options-A" class="xxinput"  placeholder="请输入选项内容" value=""><div class="xxdj">-</div></div>'
                        $('.xxitem').append(str);
                    }
                })
                $(document).on('click',"#xxzj",function(){
                    if($('.xxdj').length < 11){
                        var shuzi  = ['A','B','C','D','E','F','G','H','I','J','H'];
                        var lengths = $('.xxdj').length;
                        var str = '<div class="layui-input-block" style="margin-top: 10px"><label class="layui-form-label" style="width: 20px">'+shuzi[lengths]+'、</label><input type="text" name="options-'+shuzi[lengths]+'" class="xxinput"  placeholder="请输入选项内容" value=""><div class="xxdj">-</div></div>'
                        $('.xxitem').append(str);
                    }
                });
                $(document).on('click','.xxdj',function(){
                    var index = $('.xxdj').index(this);
                    var lengths = $('.xxdj').length;
                    if(lengths > 1){
                        if(index+1 != lengths){
                            layer.msg('请从最后一个开始删除')
                        }else {
                            $('.xxdj').eq(index).parent().remove()
                        }
                    }
                });

            });
            ea.listen();
        },
        edit: function () {
            layui.use(['form'], function() {
                var form = layui.form;
                form.on('select(type)', function(data){
                    $('.xxitem').empty()
                    if(data.value == 3){
                        var str = '<label class="layui-form-label">选项</label><div class="layui-input-block" style="margin-top: 10px;"><label class="layui-form-label" style="width: 20px">A、</label><input disabled type="text" name="options-A" class="xxinput"  placeholder="正确" value="正确"></div><div class="layui-input-block" style="margin-top: 10px"><label class="layui-form-label" style="width: 20px">B、</label> <input disabled type="text" name="options-B" class="xxinput"  placeholder="错误" value="错误"></div>'
                        $('.xxitem').append(str);
                    }else {
                        var str = '<label class="layui-form-label">选项</label><div class="layui-input-block xxzj" id="xxzj"><label class="layui-form-label" style="width: 20px">+</label></div><div class="layui-input-block" style="margin-top: 10px"><label class="layui-form-label" style="width: 20px">A、</label><input type="text" name="options-A" class="xxinput"  placeholder="请输入选项内容" value=""><div class="xxdj">-</div></div>'
                        $('.xxitem').append(str);
                    }
                })
                $(document).on('click',"#xxzj",function(){
                    if($('.xxdj').length < 11){
                        var shuzi  = ['A','B','C','D','E','F','G','H','I','J','H'];
                        var lengths = $('.xxdj').length;
                        var str = '<div class="layui-input-block" style="margin-top: 10px"><label class="layui-form-label" style="width: 20px">'+shuzi[lengths]+'、</label><input type="text" name="options-'+shuzi[lengths]+'" class="xxinput"  placeholder="请输入选项内容" value=""><div class="xxdj">-</div></div>'
                        $('.xxitem').append(str);
                    }
                });
                $(document).on('click','.xxdj',function(){
                    var index = $('.xxdj').index(this);
                    var lengths = $('.xxdj').length;
                    if(lengths > 1){
                        if(index+1 != lengths){
                            layer.msg('请从最后一个开始删除')
                        }else {
                            $('.xxdj').eq(index).parent().remove()
                        }
                    }
                });

            });
            ea.listen();
        },
    };
    return Controller;
});